package main

import (
	"github.com/veandco/go-sdl2/sdl"
)

const (
	W = 670
	H = 620
)

var (
	Gwindow  *sdl.Window
	Grender  *sdl.Renderer
	Gtexture *Texture
)

func init() {
	sdl.Init(sdl.INIT_EVERYTHING)
	Gwindow, err := sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, W, H, sdl.WINDOW_SHOWN)
	if err != nil {
		panic(err)
	}
	Grender, err = sdl.CreateRenderer(Gwindow, -1, sdl.RENDERER_ACCELERATED)
	if err != nil {
		panic(err)
	}
	Grender.SetDrawColor(128, 128, 128, 255)
	Gtexture = &Texture{}
	Gtexture.LoadFromFile("../asserts/image/texture.png")
}

func main() {
	sprits := [2]*sdl.Rect{}
	sprits[0] = &sdl.Rect{643, 3, 26, 26}
	sprits[1] = &sdl.Rect{643, 35, 26, 26}
	frame := 0

	running := true
	for running {
		for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch event.(type) {
			case *sdl.QuitEvent:
				println("Quit")
				running = false
				break
			}
		}
		Grender.Clear()
		dst := &sdl.Rect{X: 0, Y: 0, W: 40, H: 40}

		Gtexture.Render(sprits[frame], dst)
		if frame == 0 {
			frame = 1
		} else {
			frame = 0
		}

		Grender.Present()
		sdl.Delay(16)
	}
	free()
}

func free() {
	Gtexture.Free()
	Grender.Destroy()
	Gwindow.Destroy()
	sdl.Quit()
}
